package Simulator;

import java.util.List;
import java.util.Random;

public class Bird extends AbstactAnimal {
	// A shared random number generator to control breeding.
	private static final Random rand = new Random();
	// This is the number of needed pieces of grass.
	private static final int BIRD_HUNGER = 2;
	// number of steps a bird can go before it has to eat again.
	private static final int GRASS_FOOD_VALUE = 4;
	// Variables to set the maxAge, breedingAge, probability and litter
	private static int age = 30;
	private static int breed = 3;
	private static double probability = 0.12;
	private static int litter = 3;

	public Bird(boolean randomAge) {
		super();
		maxAge = age;
		breedingAge = breed;
		breedingProbability = probability;
		maxLitterSize = litter;

		if (randomAge) {
			setAge(rand.nextInt(super.maxAge));
			foodLevel = rand.nextInt(GRASS_FOOD_VALUE);
		} else {
			// leave age at 0
			foodLevel = GRASS_FOOD_VALUE;
		}
	}

	public static void setUp(int maxAge, int breedAge, double prob,
			int maxLitter) {
		age = maxAge;
		breed = breedAge;
		probability = prob;
		litter = maxLitter;
	}

	public static String getUp() {
		String x = "";
		x = age + " " + breed + " " + probability + " " + litter;
		return x;
	}

	public void act(Field currentField, Field updatedField,
			List<ActorInterface> newActors) {
		incrementAge();
		incrementHunger();
		if (isAlive()) {
			int births = breed();
			for (int b = 0; b < births; b++) {
				Bird newBird = new Bird(false);
				newActors.add(newBird);
				newBird.setLocation(updatedField
						.randomAdjacentLocation(getLocation()));
				updatedField.place(newBird);
			}
			Location newLocation = updatedField
					.freeAdjacentLocation(getLocation());
			// Only transfer to the updated field if there was a free location
			if (newLocation != null) {
				setLocation(newLocation);
				updatedField.place(this);
			} else {
				// can neither move nor stay - overcrowding
				setDead();
			}

		}
	}

	public double findFood(double grassField, double grass) {
		double grassNew = grassField;
		if (grassField > grass) {
			foodLevel = GRASS_FOOD_VALUE;
			grassNew = grassField - BIRD_HUNGER;
		}
		return grassNew;
	}

	public String toString() {
		return "Bird, age " + getAge();
	}
}
